package com.hzsd.zengbanyi.biaodan_text;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import java.io.File;
import java.text.SimpleDateFormat;

public class BaseActivity extends FragmentActivity {
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		CrashApplication application = CrashApplication.getInstance();
		application.addActivity(this);
	}

	public void showMemberToast(int time, String msg) {
		Toast.makeText(this, msg, time).show();
	}

	public void showAlertdialog(Context context, String title, String msg) {
		Builder builder = new Builder(context);
		builder.setTitle(title);
		builder.setMessage(msg);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.setCancelable(false);
		builder.create().show();
	}

	/***
	 * 判断内存卡是否存在，存在则创建目录
	 * 
	 * @param File_PATH
	 */
	public void SDcardIsExist(String File_PATH) {
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
		if (!sdCardExist) {// 如果不存在SD卡，进行提示
			showMemberToast(Toast.LENGTH_SHORT, "请插入外部SD存储卡");
		} else {// 如果存在SD卡，判断文件夹目录是否存在
			File dirFirstFile = new File(File_PATH);
			if (!dirFirstFile.exists()) {// 判断文件夹目录是否存在
				dirFirstFile.mkdirs();// 如果不存在则创建

			}
		}
	}

	/**
	 * 判断WIFI是否连接
	 * 
	 * @param context
	 * @return
	 */
	public boolean wifiState(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo wifiNetworkInfo = connectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (wifiNetworkInfo != null && wifiNetworkInfo.isAvailable()&&wifiNetworkInfo.isConnected()) {

			return true;
		}
		return false;
	}
	
	/**
	 * 格式化当前时间
	 * 
	 * @return
	 */
	public String getCurrentTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyy/MM/dd HH:mm:ss");
		return sdf.format(System.currentTimeMillis());
	}
}
